package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbUserScoreAccountMapper;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.TbUserScoreAccountExample;
import com.dongdongshop.service.UserScoreAccountService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
public class UserScoreAccountServiceImpl implements UserScoreAccountService {
    @Autowired
    private TbUserScoreAccountMapper userScoreAccountMapper;

    @Override
    public void updateScoreBy(TbUserScoreAccount scoreAccount) {
        TbUserScoreAccountExample example = new TbUserScoreAccountExample();
        TbUserScoreAccountExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(scoreAccount.getUserId());
        List<TbUserScoreAccount> tbUserScoreAccounts = userScoreAccountMapper.selectByExample(example);

        if (tbUserScoreAccounts.size()==0){
            userScoreAccountMapper.insertSelective(scoreAccount);
        }else {
            TbUserScoreAccount scoreAccount1 = tbUserScoreAccounts.get(0);

//            Long expendScore = scoreAccount.getExpendScore();
//            Long expendScore1 = scoreAccount1.getExpendScore();
            Long totalScore = scoreAccount.getTotalScore();
            Long totalScore1 = scoreAccount1.getTotalScore();


            scoreAccount.setId(scoreAccount1.getId());
            scoreAccount.setTotalScore(totalScore + totalScore1);
//            scoreAccount.setExpendScore(expendScore+expendScore1);
//            scoreAccount.setCreateTime(scoreAccount1.getCreateTime());

            userScoreAccountMapper.updateByPrimaryKeySelective(scoreAccount);
        }
    }
}
